package xin.alum.aim.redis;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Async;
import xin.alum.aim.cluster.ClusterPusher;


class RedisPusher extends ClusterPusher {

    @Qualifier("aimRedisTemplate")
    private final RedisTemplate<String, Object> redisTemplate;

    RedisPusher(RedisTemplate<String, Object> redisTemplate) {
        this.redisTemplate = redisTemplate;
    }

    protected <T> boolean channel(String channel, T d) {
        redisTemplate.convertAndSend(channel, d);
        return true;
    }

    /**
     * 积压消息处理
     */
    @Async
    @Override
    protected void pushQueue() {
        redisTemplate.executePipelined((RedisCallback<Object>) c -> {
            super.pushQueue();
            return null;
        });
    }
}
